package lk.icta.core.web.action;

import lk.icta.common.AppConstants;
import lk.icta.common.ELandRuntimeException;
import lk.icta.common.ErrorCodes;
import lk.icta.common.dao.*;
import lk.icta.common.dto.FileDuplicateWarningMessage;
import lk.icta.common.entity.District;
import lk.icta.common.service.DocumentManagementService;
import lk.icta.common.web.action.DocumentUploadAction;
import lk.icta.core.dao.BoundaryTypeDAO;
import lk.icta.core.dao.GeologicalTypeDAO;
import lk.icta.core.dao.LandUsageDAO;
import lk.icta.core.dto.ArrearsRecordDetail;
import lk.icta.core.dto.LeaseArrearsDetails;
import lk.icta.core.entity.*;
import lk.icta.core.service.AlienationRailwayManagementService;
import lk.icta.core.service.LandRailwayManagementService;
import lk.icta.core.service.TaxationManagementService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class LandRailwayAction extends DocumentUploadAction {

    private static final Logger logger = LoggerFactory.getLogger(LandRailwayAction.class);

    private final LandRailwayManagementService landRailwayManagementService;
    private final AlienationRailwayManagementService alienationRailwayManagementService;
    private final TaxationManagementService taxationManagementService;

    private final GeologicalTypeDAO geologicalTypeDAO;
    private final BoundaryTypeDAO boundaryTypeDAO;
    private final DistrictDAO districtDAO;
    private final IPWZoneDAO ipwZoneDAO;
    private final DistrictEngineerZoneDAO districtEngineerZoneDAO;
    private final RailwayProtectionZoneDAO railwayProtectionZoneDAO;
    private final RailwayStationDAO railwayStationDAO;
    private final RailwayTrackDAO railwayTrackDAO;
    private final LandUsageDAO landUsageDAO;
    private final BranchDAO branchDAO;
    private final InstituteDAO instituteDAO;

    private String districtName;
    private Map<Integer, String> districtList;

    private Map<Integer, String> geologicalTypeList;
    private Map<Integer, String> boundaryTypeList;

    private Map<Integer, String> railwayTrackList;
    private Map<Integer, String> districtEngineerZoneList;
    private Map<Integer, String> IPWZoneList;
    private Map<Integer, String> railwayProtectionZoneList;
    private Map<Integer, String> railwayStationList;
    private Map<Integer, String> leaseUsageList;
    private Map<Integer, String> branchList;
    private Map<Integer, String> instituteList;

    private List<ArrearsRecordDetail> arrearsRecordList;
    private List<String> reportYearsList;
    private List<FileDuplicateWarningMessage> warningDuplicates;



    private LandRailway land;
    private AlienationRailway alienation;
    private Taxation taxation;
    protected long landId;
    private Double hectare;


    private double taxPercent;

    private String approvalComment;
    private int editMode;
    private boolean searchSelected = false;
    private int pageNo = 0;
    private boolean resetPageNo = true;
    private String searchText1;
    private String searchText2;
    private String searchText3;
    private String searchText4;
    private List<LandRailway> landList;
    private List<AlienationRailway> leaseList;
    private List<Taxation> taxList;
    private boolean showNextButton = false;
    private boolean ignoreWarnings = false;
    // Report Commons
    private int branchId;
    private int railwayTrackId;
    private int districtId;

    // Age Analysis Report
    private int yearsBackId;
    //Total Income Report
    //private double arrearsMin;
    //private double arrearsMax;
    private List<LeaseArrearsDetails> alienationList;

    private Map<Integer, String> migrationStateList;
    private Map<Integer, String> landStateList;
    // Lease Details List Report
    private int startingVal;
    private int endingVal;
    private String startingDate;
    private String endingDate;

    // Land Details List Report
    private int landState;

    public LandRailwayAction(DocumentManagementService documentManagementService, LandRailwayManagementService landRailwayManagementService,
                             AlienationRailwayManagementService alienationRailwayManagementService, GeologicalTypeDAO geologicalTypeDAO, BoundaryTypeDAO boundaryTypeDAO,
                             DistrictDAO districtDAO, IPWZoneDAO ipwZoneDAO, DistrictEngineerZoneDAO districtEngineerZoneDAO, RailwayProtectionZoneDAO railwayProtectionZoneDAO,
                             RailwayStationDAO railwayStationDAO, RailwayTrackDAO railwayTrackDAO, LandUsageDAO landUsageDAO, BranchDAO branchDAO, TaxationManagementService taxationManagementService, InstituteDAO instituteDAO) {
        super(documentManagementService);
        this.landRailwayManagementService = landRailwayManagementService;
        this.alienationRailwayManagementService = alienationRailwayManagementService;
        //this.alienationManagementService = alienationManagementService;
        this.geologicalTypeDAO = geologicalTypeDAO;
        this.boundaryTypeDAO = boundaryTypeDAO;
        this.districtDAO = districtDAO;
        this.ipwZoneDAO = ipwZoneDAO;
        this.districtEngineerZoneDAO = districtEngineerZoneDAO;
        this.railwayProtectionZoneDAO = railwayProtectionZoneDAO;
        this.railwayStationDAO = railwayStationDAO;
        this.railwayTrackDAO = railwayTrackDAO;
        this.landUsageDAO = landUsageDAO;
        this.branchDAO = branchDAO;
        this.taxationManagementService = taxationManagementService;
        this.instituteDAO = instituteDAO;
    }


    public String pageLoad() {
        logger.debug("LandRailwayAction pageLoad");
        return SUCCESS;
    }

    public String pageLoadWithLists() {
        logger.debug("LandRailwayAction pageLoadWithLists");
        // logger.debug("Test localization {}",getText("init_land_info.label"));
        try{
        taxPercent = taxationManagementService.getRecentTaxation().getTaxPercentage();
        }catch (Exception e){
            logger.error("Unable to get taxation amount. Taxation data might not inserted by Admin");
        }
            populateBasicLists();
        return SUCCESS;
    }

    public String pageLoadArrearsReport() {
        logger.debug("LandRailwayAction pageLoad Arrears Report");
        populateBasicLists();
        populateListsForReport();
        return SUCCESS;
    }

    public String addNewTaxation(){
        taxationManagementService.addTaxation(taxation, user, language);
        taxList = taxationManagementService.getAllTaxationData();
        taxation = null;
        return SUCCESS;
    }

    public String populateTaxationData(){
        taxList = taxationManagementService.getAllTaxationData();
        return SUCCESS;
    }

    //Populate Basic Lists of Land Railway Details
    protected void populateBasicLists() {
        try {
            logger.debug("Populate Basic Lists of Land Railway Details by {}", user.getUsername());
            populateCommonLists();
            IPWZoneList = ipwZoneDAO.getAllIPWZonesByUserLanguage(language, user);
            railwayProtectionZoneList = railwayProtectionZoneDAO.getAllRailwayProtectionZonesByUserLanguage(language, user);
            railwayStationList = railwayStationDAO.getAllRailwayStationsByUserLanguage(language, user);

        } catch (Exception e) {
            logger.debug("Error: ", e.getMessage());
            e.printStackTrace();
        }
    }

    protected void populateCommonLists(){
        populateDistricts();
        geologicalTypeList = geologicalTypeDAO.getAllGeologicalTypes(language, user);
        boundaryTypeList = boundaryTypeDAO.getAllBoundaryTypesByUserLanguage(language, user);

        findDocumentCategoryList(ModuleType.INITIAL_LAND);
        districtEngineerZoneList = districtEngineerZoneDAO.getAllDistrictEngineerZonesByUserLanguage(language, user);

        railwayTrackList = railwayTrackDAO.getAllRailwayTracksByUserLanguage(language, user);
        leaseUsageList = landUsageDAO.getAllLandUsages(language, user);
        instituteList = instituteDAO.getAllLandUsages(language, user);
    }

    protected void populateListsForReport() {
        //todo : This method is not complete.Complete according to user requirements
        populateDistricts();
        branchList = new HashMap<Integer, String>();
        if (user != null && user.getBranch() != null) {
            branchList.put(user.getBranch().getId(), user.getBranch().getEnBranchName());
            if (user.getDistricts() != null && user.getDistricts().size() > 0) { //  SC

            } else {                                                             //BO and SH
                logger.debug("role  : {}", user.getRole().getName());

            }
        } else {                                                                 //SDH and DH
            logger.debug("role  : {}", user.getRole().getName());
            branchList = branchDAO.getBranchList(language);
            districtList = districtDAO.getAllDistrictNames("en", user);
        }
    }

    protected void populateDistricts() {
        if (user.getDistricts() != null && !user.getDistricts().isEmpty()) {
            districtList = new HashMap<Integer, String>();
            Set<District> districts = user.getDistricts();
            if (AppConstants.ENGLISH.equals(language)) {
                for (District d : districts) {
                    districtList.put(d.getId(), d.getEnDistrictName());
                }
            } else if (AppConstants.SINHALA.equals(language)) {
                for (District d : districts) {
                    districtList.put(d.getId(), d.getSiDistrictName());
                }
            } else if (AppConstants.TAMIL.equals(language)) {
                for (District d : districts) {
                    districtList.put(d.getId(), d.getTaDistrictName());
                }
            }
        } else {
            districtList = districtDAO.getAllDistrictNames(language, user);
        }
    }

    public String leaseDetailsList(){
        try{
            leaseList = alienationRailwayManagementService.getDetailedLeaseList(branchId, railwayTrackId, districtId, startingVal, endingVal, startingDate, endingDate, user, language);
            return SUCCESS;
        }catch (Exception e){
            return ERROR;
        }
    }

    public String landDetailsList(){
        try{
            landList = landRailwayManagementService.getDetailedLandList(branchId, railwayTrackId, districtId, startingDate, endingDate, landState, user, language);
        }catch (Exception e){
            logger.error("Error in detailed land list report");
        }
        return SUCCESS;
    }

    public String initAgeAnalysisReport(){
        //populateBasicLists();
        populateCommonLists();
        populateListsForReport();
        return SUCCESS;
    }

    public String genArrearsAgeAnalysis(){
        arrearsRecordList = alienationRailwayManagementService.getArrearsRecordList(branchId, railwayTrackId, districtId, yearsBackId);
        int cYear = Calendar.getInstance().get(Calendar.YEAR);
        reportYearsList = new ArrayList<String>();
        for(int y = yearsBackId; 0 < y; y--){
            reportYearsList.add(String.valueOf(cYear - y));
        }
        populateCommonLists();
        populateListsForReport();
        return SUCCESS;
    }


	public String initLandStatistics() {
        try {
            populateBasicLists();
            populateListsForReport();

            /*this.migrationStateList = new HashMap<Integer, String>();
            migrationStateList.put(1, getText("migrated.label"));
            migrationStateList.put(2, getText("non_migrated.label"));*/

            LandRailway.State[] las = LandRailway.State.values();
            landStateList = new HashMap<Integer, String>();
            for (int x = 1; x <= las.length; x++) {
                switch (las[x - 1]) {
                    case REJECTED:
                        landStateList.put(x, getText("rejected.label"));
                        break;
                    case APPROVED:
                        landStateList.put(x, getText("approve.label"));
                        break;
                    case PENDING:
                        landStateList.put(x, getText("pending.label"));
                        break;
                    case DATA_ENTRY:
                        landStateList.put(x, getText("data_entry.label"));
                        break;
                }
            }

            LandRailway.MigrationState[] ms = LandRailway.MigrationState.values();
            migrationStateList = new HashMap<Integer, String>();
            for (int x = 1; x <= ms.length; x++) {
                switch (ms[x - 1]) {
                    case NONE:
                        migrationStateList.put(x, getText("New"));
                        break;
                    case ACCESS_DB:
                        migrationStateList.put(x, getText("Access"));
                        break;
                    case EXCEL:
                        migrationStateList.put(x, getText("Excel"));
                }
            }

            return SUCCESS;

        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
            addActionError(getText("unable_to_load_land_details.label"));
            populateBasicLists();
            return ERROR;
        }
    }
    public String addLand() {
        logger.debug("add Land by {} ", user.getUsername());
        try {
            if(!ignoreWarnings){
                warningDuplicates = new ArrayList();
                List<LandRailway> lr = landRailwayManagementService.getLandAvailableLikeFileNo(land.getLandFileNumber(), user);
                if(lr.size() > 0){
                    for(LandRailway a : lr){
                        warningDuplicates.add(new FileDuplicateWarningMessage(getText("possible_duplicate_file_found.label"),a.getLandFileNumber(), a.getId()));
                    }

                }
                List<AlienationRailway> ar = alienationRailwayManagementService.getAlienationAvailableLikeLeaseNo(alienation.getReferenceNo(), user);
                if(ar.size() > 0){
                    for (AlienationRailway a : ar){
                        warningDuplicates.add(new FileDuplicateWarningMessage(getText("possible_duplicate_alienation.label"),a.getReferenceNo(), a.getLand().getId()));
                    }
                }

                if(warningDuplicates.size() > 0){
                    populateBasicLists();
                    return WARNING;
                }
            }
            setNullForListObjects();
            //setLandLogComment("Added land ");
            landRailwayManagementService.addLand(land, alienation, user, language);
            addActionMessage(getText("land_details_saved_successfully.label"));

            populateBasicLists();
            return SUCCESS;

        } catch (ELandRuntimeException e) {
            switch (e.getErrorCode()) {
                case ErrorCodes.UNAUTHORIZED_ACCESS:
                    addActionError(getText("permission_denied.label"));
                    break;
                case ErrorCodes.INVALID_DATA:
                    addActionError(getText("invalid_data.label"));
                    break;
            }
            populateBasicLists();
            land.setState(null);
            land.setId(0);
            land.setApprovalNote("");

            return INPUT;

        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
            addActionError(getText("unable_to_save_land_details.label"));
            populateBasicLists();
            return ERROR;
        }

    }

    public String updateLand() {
        logger.debug("updated Land by {} ", user.getUsername());
        try {
            setNullForListObjects();
            //setLandLogComment("Updated land ");
            //land.setDocumentList(getUploadedfileList(ModuleType.INITIAL_LAND));
            //landRailwayManagementService.addLand(land,alienation, user, language);
            landRailwayManagementService.updateLand(land, alienation, user, language);
            addActionMessage(getText("land_details_saved_successfully.label"));

            populateBasicLists();
            return SUCCESS;

        } catch (ELandRuntimeException e) {
            switch (e.getErrorCode()) {
                case ErrorCodes.UNAUTHORIZED_ACCESS:
                    addActionError(getText("permission_denied.label"));
                    break;
                case ErrorCodes.INVALID_DATA:
                    addActionError(getText("invalid_data.label"));
                    break;
            }
            populateBasicLists();
            return INPUT;
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
            addActionError(getText("unable_to_save_land_details.label"));
            populateBasicLists();
            return ERROR;
        }
    }

    // Detailed land page load for add or edit - load land info
    public String loadInitialInformation() {
        try {
            logger.debug("Load Initial Land information of {} by {} to Detail forms", landId, getUser().getUsername());
            try{
                taxPercent = taxationManagementService.getRecentTaxation().getTaxPercentage();
            }catch (Exception e){
                logger.error("Unable to get taxation amount. Taxation data might not inserted by Admin");
            }
            //land = landManagementService.getLandByIdUKey(landId, user);
            land = landRailwayManagementService.getLandByIdUKey(landId, user);
            //Message for migrated data
//            if (land.isMigratedRecord()) {
//                addActionMessage("This is a Migrated Land Record. Please make corrections before saving. Use the Comment for more information");
//            }

//            if (land.getInitialRecordId() != 0) {    // update detailed land
//                populateUploadedFiles(land.getId(), ModuleType.DETAILED_LAND);
//            } else {  //add detailed land
//                populateUploadedFiles(land.getId(), ModuleType.INITIAL_LAND);
//            }
            populateCommonLists();
            IPWZoneList = ipwZoneDAO.getAllIPWZoneNamesByTrackId(land.getRailwayTrack().getId(), language, user);
            railwayProtectionZoneList = railwayProtectionZoneDAO.getAllRPZoneNamesByTrackId(land.getRailwayTrack().getId(), language, user);
            railwayStationList = railwayStationDAO.getAllIPWZoneNamesByIPWId(land.getIpwZone().getId(), language, user);

            //todo: Store current Alienation Id in land table  and move this method to landAction
            //if (land.getAlienationState() != Land.AlienationState.ARCHIVED) {
            alienation = alienationRailwayManagementService.getActiveAlienationByLandId(land.getId(), user);

            if (alienation == null) {
                // alienationType = AppConstants.UN_ALIENATED;
            } else {
                //alienationType = alienation.getAlienationMethod().getId();
            }
            findDocumentCategoryList(ModuleType.INITIAL_LAND);
            //populateBasicLists();
            //todo: is this needed. If it is required include it in accessor method
            populateLandInformation();

            //populateAssociateDetail();
            //use for ORIP users
//                if (user.getRole().getId().equals(AppConstants.ORIP_USER_ROLE)) {
//                    loadDsAndGnListByLand(land);
//                }

            return INPUT;
//            } else {
//                logger.error("ARCHIVED land cannot be updated");
//                addActionError("ARCHIVED land cannot be updated");
//                return ERROR;
//            }
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
    }

    protected void setLandLogComment(String approveStr) {
        if (land.getApprovalNote() != null) {
            land.setApprovalNote(land.getApprovalNote() + "\n" + approveStr + " by :" + user.getUsername() + ", Date : " + new Date() + ".");
        } else {
            land.setApprovalNote(approveStr + " by :" + user.getUsername() + ", Date : " + new Date() + ".");
        }
    }

    protected void populateLandInformation() {
        logger.debug("Populate Basic Lists according to alienation GN Division[user name: {}]", user.getUsername());
        try {
            hectare = land.getLandExtent() * 0.002529285264;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setNullForListObjects() {

        if (land != null) {

            //Geological Type has not been selected by the user
            if (land.getGeologicalType() != null && land.getGeologicalType().getId() == 0) {
                land.setGeologicalType(null);
            }
            //North Boundary has not been selected by the user
            if (land.getNorthBoundaryType() != null && land.getNorthBoundaryType().getId() == 0) {
                land.setNorthBoundaryType(null);
            }
            //North East Boundary has not been selected by the user
            if (land.getNorthEastBoundaryType() != null && land.getNorthEastBoundaryType().getId() == 0) {
                land.setNorthEastBoundaryType(null);
            }
            //East Boundary has not been selected by the user
            if (land.getEastBoundaryType() != null && land.getEastBoundaryType().getId() == 0) {
                land.setEastBoundaryType(null);
            }
            //South East Boundary has not been selected by the user
            if (land.getSouthEastBoundaryType() != null && land.getSouthEastBoundaryType().getId() == 0) {
                land.setSouthEastBoundaryType(null);
            }
            //South Boundary has not been selected by the user
            if (land.getSouthBoundaryType() != null && land.getSouthBoundaryType().getId() == 0) {
                land.setSouthBoundaryType(null);
            }
            //South West Boundary has not been selected by the user
            if (land.getSouthWestBoundaryType() != null && land.getSouthWestBoundaryType().getId() == 0) {
                land.setSouthWestBoundaryType(null);
            }
            //West Boundary has not been selected by the user
            if (land.getWestBoundaryType() != null && land.getWestBoundaryType().getId() == 0) {
                land.setWestBoundaryType(null);
            }
            //North West Boundary has not been selected by the user
            if (land.getNorthWestBoundaryType() != null && land.getNorthWestBoundaryType().getId() == 0) {
                land.setNorthWestBoundaryType(null);
            }

            //Land location has not been selected by the user
            //if (land.getLandLocation() != null && land.getLandLocation().getId() == 0) {
            //land.setLandLocation(null);
            ///}

            //Nearest Railway station has not been selected by the user
            if (land.getNearestRailwayStation() != null && land.getNearestRailwayStation().getId() == 0) {
                land.setNearestRailwayStation(null);
            }


            //RailwayProtectionZone has not been selected by the user
            if (land.getRailwayProtectionZone() != null && land.getRailwayProtectionZone().getId() == 0) {
                land.setRailwayProtectionZone(null);
            }

            if (alienation.getLeaseUsagePrimary() != null && alienation.getLeaseUsagePrimary().getId() == 0) {
                alienation.setLeaseUsagePrimary(null);
            }

            if (alienation.getLeaseUsageSecondary() != null && alienation.getLeaseUsageSecondary().getId() == 0) {
                alienation.setLeaseUsageSecondary(null);
            }

        }


    }

    public String approveLandInformation() {
        logger.debug("Approve  Land information of land id {} by {}", land.getId(), user.getUsername());
        try {
            landRailwayManagementService.approveLandInformation(land, approvalComment, user);
            addActionMessage(getText("land_approved_successfully.label"));

            populateBasicLists();
            return SUCCESS;
        } catch (ELandRuntimeException e) {
            switch (e.getErrorCode()) {
                case ErrorCodes.UNAUTHORIZED_ACCESS:
                    addActionError(getText("permission_denied.label"));
                    break;
                case ErrorCodes.ARCHIVED_LAND_APPROVAL:
                    addActionError(getText("history_record_cannot_be_approved.label"));
                    break;
                case ErrorCodes.APPROVED_LAND_APPROVAL:
                    addActionError(getText("already_approved_land.label"));
                    break;
                case ErrorCodes.REJECTED_LAND_APPROVAL:
                    addActionError(getText("already_rejected_land.label"));
                    break;
                case ErrorCodes.ARCHIVED_ALIENATION_APPROVAL:
                    addActionError(getText("history_record_cannot_be_approved.label"));
                    break;
                case ErrorCodes.APPROVED_ALIENATION_APPROVAL:
                    addActionError(getText("already_approved_alienation.label"));
                    break;
                case ErrorCodes.REJECTED_ALIENATION_APPROVAL:
                    addActionError(getText("already_rejected_alienation.label"));
                    break;
                case ErrorCodes.ILLEGAL_STATE:
                    addActionError(getText("error_occured.label"));
                    break;
                case ErrorCodes.INCOMPLETE_LAND_APPROVAL:
                case ErrorCodes.INCOMPLETE_ALIENATION_APPROVAL:
                    addActionError(getText("incomplete_record.label"));
                    break;
                default:
                    addActionError(getText("unable_to_approve.label"));
            }
            e.printStackTrace();
            logger.error(e.getMessage());
            return ERROR;
        }
    }

    public String rejectLandInformation() {
        logger.debug("Reject  Land information of land id {} by {}", land.getId(), getUser().getUsername());
        try {
            landRailwayManagementService.rejectLandInformation(land, approvalComment, user);
            addActionMessage(getText("land_rejected_successfully.label"));
            populateBasicLists();
            return SUCCESS;
        } catch (ELandRuntimeException e) {
            switch (e.getErrorCode()) {
                case ErrorCodes.UNAUTHORIZED_ACCESS:
                    addActionError(getText("permission_denied.label"));
                    break;
                case ErrorCodes.ARCHIVED_LAND_REJECTION:
                    addActionError(getText("history_record_cannot_be_approved.label"));
                    break;
                case ErrorCodes.APPROVED_LAND_REJECTION:
                    addActionError(getText("already_approved_land.label"));
                    break;
                case ErrorCodes.REJECTED_LAND_REJECTION:
                    addActionError(getText("already_rejected_land.label"));
                    break;
                case ErrorCodes.ARCHIVED_ALIENATION_REJECTION:
                    addActionError(getText("history_record_cannot_be_approved.label"));
                    break;
                case ErrorCodes.APPROVED_ALIENATION_REJECTION:
                    addActionError(getText("already_approved_alienation.label"));
                    break;
                case ErrorCodes.REJECTED_ALIENATION_REJECTION:
                    addActionError(getText("already_rejected_alienation.label"));
                    break;
                case ErrorCodes.ILLEGAL_STATE:
                    addActionError(getText("error_occured.label"));
                    break;
                case ErrorCodes.INCOMPLETE_LAND_REJECTION:
                case ErrorCodes.INCOMPLETE_ALIENATION_REJECTION:
                    addActionError(getText("incomplete_record.label"));
                    break;
                default:
                    addActionError(getText("unable_to_approve.label"));
            }
            e.printStackTrace();
            logger.error(e.getMessage());
            return ERROR;
        }
    }

    public String searchDetailedLandUsingSearchText() {
        logger.debug("Search All Land by :{} ", user.getUsername());
        searchSelected = true;
        if (resetPageNo || pageNo == 0) {
            pageNo = 1;
        }

        try {
            landList = new ArrayList<LandRailway>();
            landList = landRailwayManagementService.searchDetailedLandsOfDSDivisionBySearchText(searchText1, searchText2, searchText3, searchText4, user, pageNo);
            // landList = landManagementService.searchAllLandsOfDSDivisionBySearchText(searchText1, searchText2, searchText3, searchText4, user, pageNo);
            logger.debug("landList size {} and {}", landList.size(), AppConstants.MAX_ROWS_PER_PAGE);
            //Next button will be shown when showNextButton=true. Fails only when last page entries = MAX_ROWS_PER_PAGE
            if (landList.size() < AppConstants.MAX_ROWS_PER_PAGE) {
                showNextButton = false;
            } else {
                showNextButton = true;
            }
            resetPageNo = true;
           /* if (dsDivisionId == 0) {
                populateDistricts();
            }*/
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return SUCCESS;
    }

    public String initDetailLandsReport() {
        try {
            populateBasicLists();

            return SUCCESS;


        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
            addActionError(getText("unable_to_load_land_details.label"));
            populateBasicLists();
            return ERROR;
        }

    }

    /*
  * Detailed lands search next page
  */
    public String searchDetailedLandUsingSearchTextNextPageId() {
        pageNo = pageNo + 1;
        logger.debug("searchDetailedLandUsingSearchTextNextPageId :{} pageNo : {}", user.getUsername(), pageNo);
        resetPageNo = false;
        searchDetailedLandUsingSearchText();
        return SUCCESS;
    }

    /*
    *Detailed lands search next page
    */
    public String searchDetailedLandUsingSearchTextBackPageId() {
        if (pageNo > 1) {
            pageNo = pageNo - 1;
        }
        logger.debug("searchDetailedLandUsingSearchTextBackPageId :{} pageNo : {}", user.getUsername(), pageNo);
        resetPageNo = false;
        searchDetailedLandUsingSearchText();
        return SUCCESS;
    }

    //used in front end for diabling the fields
    public boolean isDisableMode() {
        //2- approve mode
        logger.debug("Disable mode applied {}", editMode);
        if (!(user.getRole().getId().equals(AppConstants.SC_USER_ROLE)) ||
                (user.getRole().getId().equals(AppConstants.SC_USER_ROLE) && editMode == 2)) {
            return true;
        }
        return false;
    }

    public String queryLeaseArrearsReport(){
        populateBasicLists();
        populateListsForReport();
        return SUCCESS;
    }

    public String getDistrictName() {
        return districtName;
    }

    public void setDistrictName(String districtName) {
        this.districtName = districtName;
    }

    public Map<Integer, String> getGeologicalTypeList() {
        return geologicalTypeList;
    }

    public void setGeologicalTypeList(Map<Integer, String> geologicalTypeList) {
        this.geologicalTypeList = geologicalTypeList;
    }

    public Map<Integer, String> getBoundaryTypeList() {
        return boundaryTypeList;
    }

    public void setBoundaryTypeList(Map<Integer, String> boundaryTypeList) {
        this.boundaryTypeList = boundaryTypeList;
    }

    public Map<Integer, String> getRailwayTrackList() {
        return railwayTrackList;
    }

    public void setRailwayTrackList(Map<Integer, String> railwayTrackList) {
        this.railwayTrackList = railwayTrackList;
    }

    public Map<Integer, String> getDistrictEngineerZoneList() {
        return districtEngineerZoneList;
    }

    public void setDistrictEngineerZoneList(Map<Integer, String> districtEngineerZoneList) {
        this.districtEngineerZoneList = districtEngineerZoneList;
    }

    public Map<Integer, String> getIPWZoneList() {
        return IPWZoneList;
    }

    public void setIPWZoneList(Map<Integer, String> IPWZoneList) {
        this.IPWZoneList = IPWZoneList;
    }

    public Map<Integer, String> getRailwayProtectionZoneList() {
        return railwayProtectionZoneList;
    }

    public void setRailwayProtectionZoneList(Map<Integer, String> railwayProtectionZoneList) {
        this.railwayProtectionZoneList = railwayProtectionZoneList;
    }

    public Map<Integer, String> getRailwayStationList() {
        return railwayStationList;
    }

    public void setRailwayStationList(Map<Integer, String> railwayStationList) {
        this.railwayStationList = railwayStationList;
    }

    public Map<Integer, String> getLeaseUsageList() {
        return leaseUsageList;
    }

    public void setLeaseUsageList(Map<Integer, String> leaseUsageList) {
        this.leaseUsageList = leaseUsageList;
    }

    public LandRailway getLand() {
        return land;
    }

    public void setLand(LandRailway land) {
        this.land = land;
    }

    public Map<Integer, String> getDistrictList() {
        return districtList;
    }

    public void setDistrictList(Map<Integer, String> districtList) {
        this.districtList = districtList;
    }

    public AlienationRailway getAlienation() {
        return alienation;
    }

    public void setAlienation(AlienationRailway alienation) {
        this.alienation = alienation;
    }

    public long getLandId() {
        return landId;
    }

    public void setLandId(long landId) {
        this.landId = landId;
    }

    public Double getHectare() {
        return hectare;
    }

    public void setHectare(Double hectare) {
        this.hectare = hectare;
    }

    public String getApprovalComment() {
        return approvalComment;
    }

    public void setApprovalComment(String approvalComment) {
        this.approvalComment = approvalComment;
    }

    public int getEditMode() {
        return editMode;
    }

    public void setEditMode(int editMode) {
        this.editMode = editMode;
    }


    public boolean isSearchSelected() {
        return searchSelected;
    }

    public void setSearchSelected(boolean searchSelected) {
        this.searchSelected = searchSelected;
    }

    public boolean isResetPageNo() {
        return resetPageNo;
    }

    public void setResetPageNo(boolean resetPageNo) {
        this.resetPageNo = resetPageNo;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }


    public String getSearchText1() {
        return searchText1;
    }

    public void setSearchText1(String searchText1) {
        this.searchText1 = searchText1;
    }

    public String getSearchText2() {
        return searchText2;
    }

    public void setSearchText2(String searchText2) {
        this.searchText2 = searchText2;
    }

    public String getSearchText3() {
        return searchText3;
    }

    public void setSearchText3(String searchText3) {
        this.searchText3 = searchText3;
    }

    public String getSearchText4() {
        return searchText4;
    }

    public void setSearchText4(String searchText4) {
        this.searchText4 = searchText4;
    }

    public boolean isShowNextButton() {
        return showNextButton;
    }

    public void setShowNextButton(boolean showNextButton) {
        this.showNextButton = showNextButton;
    }

    public List<LandRailway> getLandList() {
        return landList;
    }

    public void setLandList(List<LandRailway> landList) {
        this.landList = landList;
    }

   /* public Double getArrearsMin() {
        return arrearsMin;
    }

    public void setArrearsMin(Double arrearsMin) {
        this.arrearsMin = arrearsMin;
    }*/

   /* public Double getArrearsMax() {
        return arrearsMax;
    }

    public void setArrearsMax(Double arrearsMax) {
        this.arrearsMax = arrearsMax;
    }*/

    public int getBranchId() {
        return branchId;
    }

    public void setBranchId(int branchId) {
        this.branchId = branchId;
    }

    public int getRailwayTrackId() {
        return railwayTrackId;
    }

    public void setRailwayTrackId(int railwayTrackId) {
        this.railwayTrackId = railwayTrackId;
    }

    public int getDistrictId() {
        return districtId;
    }

    public void setDistrictId(int districtId) {
        this.districtId = districtId;
    }

    public List<LeaseArrearsDetails> getAlienationList() {
        return alienationList;
    }

    public void setAlienationList(List<LeaseArrearsDetails> alienationList) {
        this.alienationList = alienationList;
    }


    public Map<Integer, String> getBranchList() {
        return branchList;
    }

    public void setBranchList(Map<Integer, String> branchList) {
        this.branchList = branchList;
    }

    public static Logger getLogger() {
        return logger;
    }
	
	public Map<Integer, String> getLandStateList() {
        return landStateList;
    }

    public String getEndingDate() {
        return endingDate;
    }

    public void setEndingDate(String endingDate) {
        this.endingDate = endingDate;
    }

    public String getStartingDate() {
        return startingDate;
    }

    public void setStartingDate(String startingDate) {
        this.startingDate = startingDate;
    }

    public int getEndingVal() {
        return endingVal;
    }

    public void setEndingVal(int endingVal) {
        this.endingVal = endingVal;
    }

    public int getStartingVal() {
        return startingVal;
    }

    public void setStartingVal(int startingVal) {
        this.startingVal = startingVal;
    }

    public List<AlienationRailway> getLeaseList() {
        return leaseList;
    }

    public void setLeaseList(List<AlienationRailway> leaseList) {
        this.leaseList = leaseList;
    }

	public void setLandStateList(Map<Integer, String> landStateList) {
        this.landStateList = landStateList;
    }

    public void setMigrationStateList(Map<Integer, String> migrationStateList) {
        this.migrationStateList = migrationStateList;
    }

    public Map<Integer, String> getMigrationStateList() {
        return migrationStateList;
    }

    public List<Taxation> getTaxList() {
        return taxList;
    }

    public void setTaxList(List<Taxation> taxList) {
        this.taxList = taxList;
    }

    public Taxation getTaxation() {
        return taxation;
    }

    public void setTaxation(Taxation taxation) {
        this.taxation = taxation;
    }

    public double getTaxPercent() {
        return taxPercent;
    }

    public int getYearsBackId() {
        return yearsBackId;
    }

    public void setYearsBackId(int yearsBackId) {
        this.yearsBackId = yearsBackId;
    }

    public List<ArrearsRecordDetail> getArrearsRecordList() {
        return arrearsRecordList;
    }

    public void setArrearsRecordList(List<ArrearsRecordDetail> arrearsRecordList) {
        this.arrearsRecordList = arrearsRecordList;
    }

    public List<String> getReportYearsList() {
        return reportYearsList;
    }

    public void setReportYearsList(List<String> reportYearsList) {
        this.reportYearsList = reportYearsList;
    }

    public int getLandState() {
        return landState;
    }

    public void setLandState(int landState) {
        this.landState = landState;
    }

    public Map<Integer, String> getInstituteList() {
        return instituteList;
    }

    public void setInstituteList(Map<Integer, String> instituteList) {
        this.instituteList = instituteList;
    }

    public boolean isIgnoreWarnings() {
        return ignoreWarnings;
    }

    public void setIgnoreWarnings(boolean ignoreWarnings) {
        this.ignoreWarnings = ignoreWarnings;
    }

    public List<FileDuplicateWarningMessage> getWarningDuplicates() {
        return warningDuplicates;
    }

    public void setWarningDuplicates(List<FileDuplicateWarningMessage> warningDuplicates) {
        this.warningDuplicates = warningDuplicates;
    }
}
